﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class FormConnectionToServer : Form
    {
        public FormConnectionToServer()
        {
            InitializeComponent();
        }

        private void frmThongTinKetNoi_Load(object sender, EventArgs e)
        {
            LoadData(); 
        }

        private void LoadData()
        {
            txtServerName.Text = Properties.Settings.Default.Server;
            txtLoginUser.Text = Properties.Settings.Default.User;
            txtPassword.Text = Properties.Settings.Default.Pass;
            txtNamedatabase.Text = Properties.Settings.Default.Databasename;
        }
        private void SaveData()
        {
            Properties.Settings.Default.Server = txtServerName.Text;
            Properties.Settings.Default.User = txtLoginUser.Text;
            Properties.Settings.Default.Pass = txtPassword.Text;
            Properties.Settings.Default.Databasename = txtNamedatabase.Text;
            Properties.Settings.Default.VIBU_StoreConnectionString = @"Data Source=" + txtServerName.Text + ";Initial Catalog=" + txtNamedatabase.Text + ";User ID=" + txtLoginUser.Text + ";Password=" + txtPassword.Text + "";
            Properties.Settings.Default.Save(); 
        }
        private List<string> TestDataInput()
        {
            List<string> lstError = new List<string>();
            if (string.IsNullOrEmpty(txtServerName.Text)==true) lstError.Add("Chưa nhập server name!");
            return lstError; 
        }


        #region  event
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnSaveInforConnect_Click(object sender, EventArgs e)
        {
            if (TestDataInput().Count == 0)
            {
                SaveData();
                MessageBox.Show("Đã lưu lại!");
            }
            else MessageBox.Show("Lỗi!"); 
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (new Controler().TestConnection(txtServerName.Text, txtLoginUser.Text, txtPassword.Text, txtNamedatabase.Text) == true)
                MessageBox.Show("Kết nối thành công!");
            else MessageBox.Show("Không kết nối được!");
        }
        #endregion 

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
